#ifndef MIRRORPICTUREITEM_H
#define MIRRORPICTUREITEM_H

#include <QGraphicsRectItem>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QPixmap>
#include <QStateMachine>
#include <QTimeLine>
#include <QDebug>
#include <QPropertyAnimation>
#include "PictureLoaderThread.h"

enum MirrorStyle
{
    NoMirror,
    MirrorOverX,
    MirrorOverY
};

enum FadeStyle
{
    NoFade,
    FadeDown,
    FadeUp,
    FadeRight,
    FadeLeft
};

inline QImage mirrorImage (const QImage &img,
                    MirrorStyle mirrorStyle=MirrorOverX,
                    FadeStyle fadeStyle=FadeDown)
{
    /* Reverse image */
    QImage tmpImage = img;
    if (mirrorStyle != NoMirror)
        tmpImage = tmpImage.mirrored(mirrorStyle == MirrorOverY,
                                     mirrorStyle == MirrorOverX);

    /*Add gradient to mirrored image.*/
    if (fadeStyle != NoFade) {
        QPoint p1, p2;

        if (fadeStyle == FadeDown)
            p2.setY(tmpImage.height());
        else if (fadeStyle == FadeUp)
            p1.setY(tmpImage.height());
        else if (fadeStyle == FadeRight)
            p2.setX(tmpImage.width());
        else if (fadeStyle == FadeLeft)
            p1.setX(tmpImage.width());

        QLinearGradient gradient(p1, p2);
        gradient.setColorAt(0, QColor(0, 0, 0, 100));
        gradient.setColorAt(1, Qt::transparent);

        QPainter p(&tmpImage);
        p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        p.fillRect(0, 0, tmpImage.width(), tmpImage.height(), gradient);
        p.end();
    }

    return tmpImage;


}


class MirrorPictureItem : public QObject , public QGraphicsRectItem
{
    Q_OBJECT
    // Change picture position
    Q_PROPERTY(QRectF rect READ rect WRITE setRect)
    Q_PROPERTY(QPointF pos READ pos WRITE setPos)
    Q_PROPERTY(qreal scale READ scale WRITE setScale)
public:
    explicit MirrorPictureItem( PictureData* picData, QGraphicsItem * parent = 0);
    virtual ~MirrorPictureItem(){}

    void initAnimations(int nCount, int midCount, float itemWidth);

    QRectF boundingRect () const;
    void paint ( QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *  );
    bool isAnimFinished();
    void startSelectAnim();
signals:
    void midChange(int midIndex, bool bSelect = false);
    void select(PictureData* data);
protected:
    virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
    virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
    virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

public slots:
    void updateStep_l(qreal step);
    void updateStep_m(qreal step);
    void updateStep_r(qreal step);
    void scaleAnimFinish();
    void rectAnimFinish();
    void rectAnimInvFinish();

private:
    PictureData*  mPicData;
    QImage   mMirrorImage;
    QPainter mPointer;
    QPointF  mPressedPos;
    QPointF  mReleasedPos;
    int      mCount;

    QPropertyAnimation* mScaleAnim;
    QPropertyAnimation* mPosAnim;
    QPropertyAnimation* mPosSelAnim;
    QPropertyAnimation* mRectAnim;
    QPropertyAnimation* mRectAnimInv;

    QTimeLine *mTimeLine;
    QTimeLine *mTimeLineInv;
    QTransform mTransforms[2];
    QTransform mCurTransform;

    bool mIsSelected;
    qreal   mScale;
    QPointF  mOffset;
    //QGradient*   mGradient;
};

#endif // MIRRORPICTUREITEM_H
